package com.steven.ourcontest.main.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.steven.ourcontest.base.BaseViewModel
import com.steven.ourcontest.main.bean.TeamTop5DetailRes
import com.steven.ourcontest.team.api.TeamApi
import com.steven.ourcontest.team.bean.TeamInfoRes
import com.steven.ourcontest.utils.plusAssign

class MainViewModel : BaseViewModel() {
    private val _recruitTeam = MutableLiveData<TeamInfoRes?>()
    val recruitTeam: LiveData<TeamInfoRes?>
        get() = _recruitTeam

    private val _top5Team = MutableLiveData<TeamTop5DetailRes>()
    val top5Team: LiveData<TeamTop5DetailRes>
        get() = _top5Team

    private var lastCreateTime = Long.MAX_VALUE
    fun queryRecruitData() {
        disposed += TeamApi.instance.searchNewCreateTeam(lastCreateTime)
            .subscribe({
                _recruitTeam.value = it
                lastCreateTime = it.data.last().createTime
            }, {
                _recruitTeam.value = null
                it.printStackTrace()
            })
    }

    fun queryTop5Team() {
        disposed += TeamApi.instance.searchTop5Team()
            .subscribe({
                _top5Team.value = it
            }, {
                it.printStackTrace()
            })
    }
}